package com.taxshare.purchaser.util.batchHandlerUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName: BatchHandlerUtil
 * @Description: 分批进行数据库插入
 * @author: hjh
 * @date: 2021/4/12 16:53
 */
@Component
public class BatchHandlerUtil<T> {
    /**
     *  通过自定义分批参数    例如list.size() = 10000  ； batchSize = 200  ； 分50组进行resolve 操作
     * @param list 需要拆分的集合
     * @param batchSize 拆分大小
     * @param resolve
     */
    public void  batchResolve(List<T> list, int batchSize, CommonResolve resolve) {
        //集合为空直接返回
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        for(int i = 0; i < list.size(); i += batchSize) {
            int end = i + batchSize;
            if(end > list.size()) {
                end = list.size();
            }
            List<T> items = list.subList(i, end);
            resolve.resolve(items);
        }
    }
}
